package com.eastfair.home.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.StaticContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.service.HomeViolationRecordService;
import com.eastfair.messagecenter.util.SendMsgUtil;
import com.eastfair.venuebooking.api.ViolationRecordFeign;
import com.eastfair.venueservice.dto.ViolationRecordDTO;
import com.eastfair.venueservice.enumeration.ViolationRecordViolationSourceEnum;
import com.eastfair.venueservice.vo.ViolationRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Struct;

/**
 * 违规记录 service.
 *
 * @Author lyw
 * @Date 2022 /9/13 20:26
 */
@Slf4j
@Service
public class HomeViolationRecordServiceImpl implements HomeViolationRecordService {

    @Resource
    private ViolationRecordFeign violationRecordFeign;

    @Resource
    private SendMsgUtil sendMsgUtil;

    @Override
    public Page<ViolationRecordVO> queryViolationRecordList(PageParams<ViolationRecordDTO> params) {
        log.info("queryViolationRecordList - 查询违规惩罚记录列表分页, params={}", params);
        ViolationRecordDTO model = params.getModel();
        model.setViolationSource(ViolationRecordViolationSourceEnum.HOME_CHECK);
        R<Page<ViolationRecordVO>> r = violationRecordFeign.queryViolationRecordList(params);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public ViolationRecordVO getViolationRecord(ViolationRecordDTO violationRecordDTO) {
        log.info("getViolationRecord - 查询违规惩罚记录详情, violationRecordDTO={}", violationRecordDTO);
        R<ViolationRecordVO> r = violationRecordFeign.getViolationRecord(violationRecordDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public boolean saveViolationRecord(ViolationRecordDTO violationRecordDTO) {
        log.info("saveViolationRecord - 保存违规惩罚记录, violationRecordDTO={}", violationRecordDTO);
        // 所属系统
        violationRecordDTO.setViolationSource(ViolationRecordViolationSourceEnum.HOME_CHECK);
        R<Boolean> r = violationRecordFeign.saveForHome(violationRecordDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public boolean updateViolationRecord(ViolationRecordDTO violationRecordDTO) {
        log.info("updateViolationRecord - 修改违规惩罚记录, violationRecordDTO={}", violationRecordDTO);
        R<Boolean> r = violationRecordFeign.updateViolationRecord(violationRecordDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public boolean updatePunishAmount(ViolationRecordDTO violationRecordDTO) {
        log.info("updatePunishAmount - 更新处罚扣款, violationRecordDTO={}", violationRecordDTO);
        R<Boolean> r = violationRecordFeign.updatePublishAmount(violationRecordDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public boolean updateViolationCancel(Long id) {
        log.info("updateViolationCancel - 撤销处罚, id={}", id);
        R<Boolean> r = violationRecordFeign.updateViolationCancel(id);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public boolean delViolationRecord(Long id) {
        log.info("delViolationRecord - 删除违规惩罚记录, id={}", id);
        R<Boolean> r = violationRecordFeign.delViolationRecord(id);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public boolean sendConfigPayeeInnerMsg(ViolationRecordDTO violationRecordDTO) {
        log.info("sendConfigPayeeInnerMsg - 发送配置收款商户的站内信, violationRecordDTO={}", violationRecordDTO);
        String orderType = "违规记录";
        if (violationRecordDTO.getSupervisorId() == null || StrUtil.isBlank(violationRecordDTO.getSupervisorName())) {
            log.warn("违规记录的罚款单位名称或ID不存在, superVisorId={}, supervisorName={}", violationRecordDTO.getSupervisorId(), violationRecordDTO.getSupervisorName());
        }
        String content = String.format("主场【%s】，您好，%s已生成，如需收款请到财务系统配置收款账号！", violationRecordDTO.getSupervisorName(), orderType);
        sendMsgUtil.sendInnerMsgByTeamId(content, violationRecordDTO.getSupervisorId(), StaticContextUtil.getProjectSystemScene());
        return true;
    }

}
